package com.lge.pitchingcoins.util.sessions;

import java.util.ArrayList;
import java.util.concurrent.BlockingQueue;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import com.lge.pitchingcoins.util.PitchingCoinsLog;
import com.lge.pitchingcoins.util.sessions.GameCmdManager.GameCmdBase;
import com.lge.pitchingcoins.util.sessions.GameCmdManager.GameCmdSendDTO;

public class GameCmdProcessor {

    private ArrayList<GameCmdBase> mCmdList = null;
    private Thread mWorker = null;

    /**
     * SingletonHolder
     */
    private static class SingletonHolder {
        public static final GameCmdProcessor INSTANCE = new GameCmdProcessor();
    }

    public static GameCmdProcessor getInstance() {
        return SingletonHolder.INSTANCE;
    }

    private GameCmdProcessor() {
        mCmdList = new ArrayList<GameCmdBase>();
    }

    public ArrayList<GameCmdBase> getCmdList() {
        return mCmdList;
    }

    public boolean putCmd(GameCmdBase cmd) {
        mCmdList.add(cmd);
        return true;
    }

    public void startProcessing() {
        if (mWorker != null) {
            return;
        }

        PitchingCoinsLog.d("startProcessing start");
        mWorker = new Thread() {
            public void run() {
                PitchingCoinsLog.d("startProcessing run");
                BlockingQueue<GameCmdBase> bq;
                GameCmdBase cmd;
                //				int remainCount = 0;

                bq = GameCmdManager.getInstance().getCmdQueue();
                while (true) {
                    PitchingCoinsLog.d("while");
                    try {
                        PitchingCoinsLog.d("run");
                        cmd = bq.take();

                        //						remainCount = bq.size();
                        //						PitchingCoinsLog.d("" + remainCount);

                        handleCommand(cmd);

                    } catch (InterruptedException e) {
                        PitchingCoinsLog.d("while exception");
                        e.printStackTrace();
                    }
                }
            }
        };
        mWorker.start();
    }

    //	private final int GAME_PLAY_STATUS_CMD = 0;

    private boolean handleCommand(GameCmdBase cmd) {
        PitchingCoinsLog.d("handleCommand out");

        //		GameCmdSendDTO playCmd = (GameCmdSendDTO) message.obj;
        GameCmdSendDTO playCmd = (GameCmdSendDTO)cmd;

        
        if (mOnGameDataCmdListener != null) {
            PitchingCoinsLog.d("mOnGameDataCmdListener != null");
            mOnGameDataCmdListener.onGameDataCmd(playCmd.getUserName(),
                    playCmd.getX(), playCmd.getY(), playCmd.getZ(), playCmd.getForce());

        }

        //		if (cmd instanceof GameCmdSendDTO) {
        //			PitchingCoinsLog.d("handleCommand");
        //			mGameCmdHandler.sendMessage(mGameCmdHandler.obtainMessage(
        //					GAME_PLAY_STATUS_CMD, cmd));
        //		} else {
        //			return false;
        //		}

        return true;

    }

    //	private Handler mGameCmdHandler = new Handler(Looper.getMainLooper()) {
    //		public void handleMessage(Message message) {
    //			switch (message.what) {
    //			case GAME_PLAY_STATUS_CMD:
    //				PitchingCoinsLog.d("mGameCmdHandler");
    //				GameCmdSendDTO playCmd = (GameCmdSendDTO) message.obj;
    //
    //				PitchingCoinsLog.d("playCmd.userName : " + playCmd.userName);
    //				PitchingCoinsLog.d("playCmd.x : " + playCmd.mX);
    //				PitchingCoinsLog.d("playCmd.y : " + playCmd.mY);
    //				PitchingCoinsLog.d("playCmd.z : " + playCmd.mZ);
    //				PitchingCoinsLog.d("playCmd.force : " + playCmd.force);
    //
    //				mOnGameDataCmdListener.onGameDataCmd(playCmd.userName,
    //						playCmd.mX, playCmd.mY, playCmd.mZ, playCmd.force);
    //				break;
    //			default:
    //				break;
    //			}
    //		};
    //	};

    public interface onGameDataCmdListener {
        void onGameDataCmd(String userName, float x, float y, float z, int force);
    }

    private onGameDataCmdListener mOnGameDataCmdListener;

    public void setOnGameDataCmdListener(onGameDataCmdListener l) {
        mOnGameDataCmdListener = l;
    }

}
